package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.basicframework.ui.recyclerview.RecyclerViewIndicator;
import com.thrd.taiexhibition.BaseApplication;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.adapter.StoryAdapter;
import com.thrd.taiexhibition.bean.BeanStory;
import com.thrd.taiexhibition.bean.BeanStoryType;
import com.thrd.taiexhibition.bean.CityEvent;
import com.thrd.taiexhibition.presener.CityStoryPresener;
import com.thrd.taiexhibition.utils.JumpUtils;
import com.thrd.taiexhibition.view.CityStoryView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.List;

/**
 * 城市故事
 * @author: ${唐杰}
 * @date: 2021/3/18
 */
public class CityStoryFragment extends BaseFragment implements CityStoryView {

    private CityStoryPresener mCityStoryPresener;
    private RecyclerViewIndicator mRecyclerViewIndicator;
    private StoryAdapter mStoryAdapter;
    private long cityId;
    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_city_story;
    }

    @Override
    protected void setUpView(View mContentView) {
        mRecyclerViewIndicator = mContentView.findViewById(R.id.mRecyclerView);
        LinearLayoutManager manager=   new LinearLayoutManager(getMContext());
        manager.setOrientation(RecyclerView.VERTICAL );
        mRecyclerViewIndicator.setLayoutManager(manager);




    }

    @Override
    protected void setUpData() {
        mCityStoryPresener = new CityStoryPresener(this);
        mStoryAdapter = new StoryAdapter(getMContext(),null);
        mRecyclerViewIndicator.setAdapter(mStoryAdapter);


        mStoryAdapter.setOnClickBeanStory(new StoryAdapter.OnClickBeanStory() {
            @Override
            public void setOnClickBeanStory(BeanStory mBeanStory) {
                JumpUtils.JumpUtils(mBeanStory.getSkipType(),mBeanStory.getOneRelevanceId());
            }
        });
    }


    @Override
    public void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mCityStoryPresener!=null&&beanStoryTypeList==null&&mBaseApplication!=null){
            mCityStoryPresener.getCityStorData((int) (cityId= mBaseApplication.getCityId()));
        }else if(beanStoryTypeList!=null){
            onSuccess(beanStoryTypeList);
        }

    }

    @Override
    public void onError(String error, int type) {
        getContentView().setState(LoadingState.STATE_ERROR);
    }

    private List<BeanStoryType> beanStoryTypeList;
    @Override
    public void onSuccess(List<BeanStoryType> beanStoryTypeList) {
        this.beanStoryTypeList=beanStoryTypeList;
        mStoryAdapter.refresh(beanStoryTypeList);
        mRecyclerViewIndicator.forceUpdateItemCount();
        getContentView().setState(LoadingState.STATE_SUCCESS);
    }

    @Override
    protected void onRecoveryBundle(Bundle savedInstanceState) {
        super.onRecoveryBundle(savedInstanceState);
        if(savedInstanceState!=null){
            BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
            if(mBaseApplication!=null){
                long cityID=savedInstanceState.getLong("cityId");
                if(mBaseApplication.getCityId()==cityID){
                    beanStoryTypeList= ( List<BeanStoryType>) savedInstanceState.getSerializable("BeanStoryTypeList");
                }
            }

        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("BeanStoryTypeList", (Serializable) beanStoryTypeList);
        outState.putSerializable("cityId",  cityId);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        beanStoryTypeList=null;
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchCity(CityEvent mCityEvent){
        if(mCityEvent!=null&&mCityStoryPresener!=null){
            beanStoryTypeList=null;
            mCityStoryPresener.getCityStorData((int) (cityId= mCityEvent.getCityId()));
        }

    }
}
